package com.study.get.jsl.ths;

import cn.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Test;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author zhuzi
 * 2025/2/7 上午8:49
 */
public class Key2Demo {


    @Test
    public void testV() {
        for (int i = 0; i < 20; i++) {
            String s = RandomUtil.randomString(60);
            System.out.println(s);
           /* String s = thsCookie();
            System.out.println(s);
            System.out.println(s.length());*/
        }

    }

    private static String thsCookie() {
        // 模拟随机数据和哈希过程
        String data = generateRandomData();
        return hashData(data);
    }

    private static String generateRandomData() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 32; i++) {
            sb.append((char) (random.nextInt(26) + 'a'));
        }
        return sb.toString();
    }

    private static String hashData(String data) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(data.getBytes());
            return bytesToHex(hashBytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256 algorithm not found", e);
        }
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}
